package cn.edu.scau.cmi.yangqing.hibernate.dao;

import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;

import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Tutor;
import cn.edu.scau.cmi.yangqing.hibernate.util.HibernateUtil;
import javafx.scene.shape.QuadCurve;

public class TutorDao {
	
	//TutorDao tutorDao = new TutorDao();
	
	//添加导师
	public void addTutor(Tutor tutor) {
		System.out.println("TutorDao:"+tutor);
		Session session = HibernateUtil.getCurrentSession();
		/*
		 * String sql = "insert into tutor (id,name) values (?,?) "; NativeQuery query =
		 * session.createSQLQuery(sql); query.setParameter(1, tutor.getId());
		 * query.setParameter(2, tutor.getName());
		 */
		session.save(tutor);
		
	}
    
	public void deleteTutor(Integer id) {
		System.out.println("deleteTutorDao");
		Session session = HibernateUtil.getCurrentSession();
		TutorDao tutorDao = new TutorDao();
		Tutor tutor = new Tutor();
		tutor.setId(id);
		session.delete(tutor);
	}
	
	public Tutor findTutorById(Integer id) {
		System.out.println("findTutorById");
		Session session = HibernateUtil.getCurrentSession();
		String sql = "select * from tutor where id = ?";
		NativeQuery query = session.createSQLQuery(sql);
		query.setParameter(1,id);
		query.addEntity(Tutor.class);
		Tutor temp = (Tutor) query.uniqueResult();
		return temp;
	}

	public void updateTutor(Tutor tutor) {
		System.out.println("updateTutor");
		System.out.println(tutor);
		Session session = HibernateUtil.getCurrentSession();
		session.update(tutor);
//		String sql = "update tutor set name=? where id=?";
//		NativeQuery query = session.createSQLQuery(sql);
//		query.setParameter(1,tutor.getId());
//		query.setParameter(2, tutor.getName());
//		query.addEntity(Tutor.class);
//		query.executeUpdate();
	}

	public List<Tutor> findAllTutor() {
		System.out.println("findAllTutor");
		Session session = HibernateUtil.getCurrentSession();
		String sql = "select * from tutor ";
		NativeQuery query = session.createSQLQuery(sql);
		query.addEntity(Tutor.class);
		List<Tutor> list = query.getResultList();
		return list;
	}

	public List<Student> teachStudentList(Integer id) {
		System.out.println("teachStudentList");
		Session session = HibernateUtil.getCurrentSession();
		String sql = "select * from student where tutorid =? ";
		NativeQuery query = session.createSQLQuery(sql);
		query.setParameter(1,id);
		query.addEntity(Student.class);
		List<Student> list = query.getResultList();
		return list;
	}

	public List<Student> teachNoStudentList(Integer id) {
		System.out.println("teachStudentList");
		Session session = HibernateUtil.getCurrentSession();
		String sql = "select * from student where tutorid!=? ";
		NativeQuery query = session.createSQLQuery(sql);
		query.setParameter(1,id);
		query.addEntity(Student.class);
		List<Student> list = query.getResultList();
		return list;
	}
	
	public void teachStudent(Integer tutorid, Integer studentid) {
		System.out.println("teachStudent");
		Session session = HibernateUtil.getCurrentSession();
		String sql = "update student set tutorid=? where id=?";
		NativeQuery query = session.createSQLQuery(sql);
		query.setParameter(1,tutorid);
		query.setParameter(2, studentid);
		query.addEntity(Student.class);
		query.executeUpdate();
	}

	public void teachNoStudent(Integer studentid) {
		System.out.println("teachNoStudent");
		Session session = HibernateUtil.getCurrentSession();
		String sql = "update student set tutorid=1 where id=?";
		NativeQuery query = session.createSQLQuery(sql);
		query.setParameter(1, studentid);
		query.addEntity(Student.class);
		query.executeUpdate();
	}
}
